package com.hustzp.com.xichuangzhu.me;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.FindCallback;
import com.hustzp.com.xichuangzhu.R;
import com.hustzp.com.xichuangzhu.utils.Constant;

import java.util.ArrayList;
import java.util.List;

import in.srain.cube.views.loadmore.LoadMoreContainer;
import in.srain.cube.views.loadmore.LoadMoreHandler;
import in.srain.cube.views.loadmore.LoadMoreListViewContainer;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;

/**
 * Created by Administrator on 2016/6/5.
 */
public class RecmdsListActivity extends MyBaseActivity {

    private PtrFrameLayout mPtrFrameLayout;
    private LoadMoreListViewContainer loadMoreListViewContainer;

    private List<AVObject> recmds = new ArrayList<>();

    private ListView recmdsListView;
    private RecmdAdapter recommandAdapter;
    private int pageIndex = 1;
    private int pageSize = 15;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_list_view);

        initToolbar();
        initViews();
    }

    private void initToolbar() {
        TextView appTitle = (TextView) findViewById(R.id.tv_title_txt);
        appTitle.setText("推荐收录");

        TextView actionText = (TextView) findViewById(R.id.tv_action_txt);
        actionText.setVisibility(View.VISIBLE);
        actionText.setText(getString(R.string.recomment_collenction_editor));
        actionText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(RecmdsListActivity.this, AddRecmdActivity.class), Constant.REQ_CODE_ADD_RECMD);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constant.REQ_CODE_ADD_RECMD && resultCode == RESULT_OK) {
            mPtrFrameLayout.autoRefresh();
        }
    }

    private void initViews() {
        recmdsListView = (ListView) findViewById(R.id.list_view);
        recommandAdapter = new RecmdAdapter(recmds);
        recmdsListView.setAdapter(recommandAdapter);

        mPtrFrameLayout = (PtrFrameLayout) findViewById(R.id.load_more_list_view_ptr_frame);

        mPtrFrameLayout.setLoadingMinTime(1000);
        mPtrFrameLayout.setPtrHandler(new PtrHandler() {
            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, recmdsListView, header);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                pageIndex = 1;
                loadData(pageIndex, pageSize);
            }
        });

        loadMoreListViewContainer = (LoadMoreListViewContainer) findViewById(R.id.load_more_list_view_container);
        loadMoreListViewContainer.useDefaultHeader();
        loadMoreListViewContainer.setLoadMoreHandler(new LoadMoreHandler() {
            @Override
            public void onLoadMore(LoadMoreContainer loadMoreContainer) {
                //加载更多操作
                pageIndex++;
                loadData(pageIndex, pageSize);
            }
        });
        loadData(pageIndex, pageSize);
    }

    private void loadData(final int pIndex, int pSize) {
        AVQuery query = AVQuery.getQuery("RecommendWork");
        query.setCachePolicy(AVQuery.CachePolicy.NETWORK_ELSE_CACHE);
        query.limit(pSize);
        query.skip((pIndex - 1) * pSize);
        query.orderByDescending("processing");
        query.orderByDescending("createdAt");
        query.findInBackground(new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> list, AVException e) {
                if (list != null && !list.isEmpty()) {
                    if (pageIndex == 1) {
                        mPtrFrameLayout.refreshComplete();
                        recmds.clear();
                    }
                    recmds.addAll(list);
                    recommandAdapter.notifyDataSetChanged();
                    loadMoreListViewContainer.loadMoreFinish(false, true);
                } else {
                    pageIndex--;
                    loadMoreListViewContainer.loadMoreFinish(false, false);
                }
            }
        });
    }

    class RecmdAdapter extends BaseAdapter {

        private List data;

        public RecmdAdapter(List data) {
            this.data = data;
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            RecmdViewHolder holder = null;
            if (convertView == null) {
                convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_my_recmd_item, parent, false);
                holder = new RecmdViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (RecmdViewHolder) convertView.getTag();
            }

            AVObject itemData = (AVObject) data.get(position);
            holder.recmdTitle.setText(itemData.getString("title"));
            holder.recmdAuthor.setText(itemData.getString("author"));

            if (itemData.getBoolean("processing")) {
                holder.recmdHint.setText("待处理");
                holder.recmdHint.setTextColor(Color.parseColor("#808080"));
                holder.recmdHint.setBackgroundResource(R.drawable.recmd_todo_style);
            } else {
                if (itemData.getBoolean("passed")) {
                    holder.recmdHint.setText("已收录");
                    holder.recmdHint.setTextColor(Color.parseColor("#4cae4c"));
                    holder.recmdHint.setBackgroundResource(R.drawable.recmd_done_style);
                } else {
                    holder.recmdHint.setText(getString(R.string.recomment_collenction_not));
                    holder.recmdHint.setTextColor(Color.parseColor("#BBBBBB"));
                    holder.recmdHint.setBackgroundResource(R.drawable.recmd_undo_style);
                }
            }

            return convertView;
        }

        class RecmdViewHolder {
            TextView recmdTitle;
            TextView recmdAuthor;
            TextView recmdHint;

            public RecmdViewHolder(View itemView) {
                recmdTitle = (TextView) itemView.findViewById(R.id.item_name);
                recmdAuthor = (TextView) itemView.findViewById(R.id.item_desc);
                recmdHint = (TextView) itemView.findViewById(R.id.tv_hint);
            }
        }
    }
}
